<?php
namespace app\Admin\controller;
use app\Admin\model\Note as NoteModel;
use app\admin\controller\Base;

class Note extends Base
{
    public function index()
    {
    	$list = db('note')->field('n.*,c.catename,u.username')->alias('n')->join('cate c','n.cateid = c.id')->join('user u','n.uid = u.id')->order('n.id desc')->paginate(2);
        // echo '<pre>';
        // print_r($list);
        // die();
        $this->assign('list',$list);
        return view();
    }

    public function check()
    {
        $id = input('id');
        $note = db('note')->find($id);
        if ($note['status'] == 1) {
            $data = [
                'id' => $id,
                'status' => 0,
            ];
            db('note')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'status' => 1,
            ];
            db('note')->update($data);
            $this->redirect('index');
        }
    }
    public function edit(){
    	$id = input('id');
    	$notes = db('note')->find($id);
        // var_dump($notes);
        $user = db('user')->where('id', $notes['uid'])->find();
        $cate = db('cate')->where('id', $notes['cateid'])->find();
        // var_dump($user);
        // die();
    	if(request()->isPost()){
    		$data=[
    			'id'=>input('id'),
                'type'=>input('type'),
    		];
            // var_dump($data);
            // die();
            if(input('type')=='on'){
                $data['type']=1;
            }else{
                $data['type']=0;
            }
    		if(db('note')->update($data)){
    			$this->success('修改帖子成功！','index');
    		}else{
    			$this->error('修改帖子失败！');
    		}
    		return;
    	}
        $this->assign('notes',$notes);
    	$this->assign('cate',$cate);
        $this->assign('user',$user);
    	// var_dump($cateres);
        // die();
        return $this->fetch();
    }

    public function del(){
    	$id=input('id');
		if(db('note')->delete(input('id'))){
			$this->success('删除成功！','index');
		}else{
			$this->error('删除失败！');
		}
    	
    }



}
